﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using CreatePerformanceCounters;
using Moq;

namespace CreatePerformanceCounters.Test
{
    /// <summary>
    /// Summary description for ProgramTest
    /// </summary>
    [TestClass]
    public class ProgramTest
    {
        private Mock<IPerformanceCounterController> _mock = null;
        public ProgramTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        TextWriter oldConsole;
        StringWriter newConsole;

        private void MockInitialize()
        {
            _mock = new Mock<IPerformanceCounterController>();
        }

        private void ConsoleInitialize()
        {
            oldConsole = Console.Out;
            newConsole = new StringWriter();
            Console.SetOut(newConsole);
        }

        [TestInitialize]
        public void Initialize()
        {
            MockInitialize();
            ConsoleInitialize();
        }


        [TestCleanup]
        public void Cleanup()
        {
            MockCleanup();
            ConsoleCleanup();
        }

        private void MockCleanup()
        {
            _mock = null;
        }

        private void ConsoleCleanup()
        {
            Console.SetOut(oldConsole);
            Console.Write(newConsole);
        }

        [TestMethod]
        public void StartWithEmptyParam()
        {
            Program.MainWithHelper(new string[] {},
                _mock.Object);

            Assert.IsTrue(newConsole.ToString().Contains(Messages.HelpMessage));
            _mock.Verify(m => m.Create(It.IsAny<string>()), Times.Never());
            _mock.Verify(m => m.Validate(It.IsAny<string>()), Times.Never());
        }

        [TestMethod]
        public void StartWithNullParam()
        {
            Program.MainWithHelper(null,
                _mock.Object);

            Assert.IsTrue(newConsole.ToString().Contains(Messages.HelpMessage));
            _mock.Verify(m => m.Create(It.IsAny<string>()), Times.Never());
            _mock.Verify(m => m.Validate(It.IsAny<string>()), Times.Never());
        }

        [TestMethod]
        public void StartWithNotRecognizedParam()
        {
            Program.MainWithHelper(new string[] { TestHelper.NotRecognizedParam },
                _mock.Object);

            Assert.IsTrue(newConsole.ToString().Contains(Messages.NotRecognizedParamMessage));
            Assert.IsTrue(newConsole.ToString().Contains(Messages.HelpMessage));
            _mock.Verify(m => m.Create(It.IsAny<string>()), Times.Never());
            _mock.Verify(m => m.Validate(It.IsAny<string>()), Times.Never());
        }

        [TestMethod]
        public void ShowHelp()
        {
            Program.MainWithHelper(new string[] { TestHelper.HelpParam }, 
                _mock.Object);

            Assert.IsTrue(newConsole.ToString().Contains(Messages.HelpMessage));
            _mock.Verify(m => m.Create(It.IsAny<string>()), Times.Never());
            _mock.Verify(m => m.Validate(It.IsAny<string>()), Times.Never());
        }

        [TestMethod]
        public void ValidateWithValidString()
        {
            Program.MainWithHelper(new string[] { TestHelper.ValidateParam, TestHelper.ValidString },
                _mock.Object);

            Assert.IsTrue(newConsole.ToString().Contains(Messages.ValidatedMessage));
            _mock.Verify(m => m.Create(It.IsAny<string>()), Times.Never());
            _mock.Verify(m => m.Validate(TestHelper.ValidString), Times.Once());
        }

        [TestMethod]
        public void ValidateWithNotValidString()
        {
            _mock.Setup(m => m.Validate(TestHelper.NotValidString)).Throws(new ArgumentException());

            Program.MainWithHelper(new string[] { TestHelper.ValidateParam, TestHelper.NotValidString },
                _mock.Object);

            Assert.IsTrue(newConsole.ToString().Contains(Messages.NotValidatedMessage));
            _mock.Verify(m => m.Create(It.IsAny<string>()), Times.Never());
            _mock.Verify(m => m.Validate(TestHelper.NotValidString), Times.Once());
        }

        [TestMethod]
        public void CreateWithValidString()
        {
            Program.MainWithHelper(new string[] { TestHelper.CreateParam, TestHelper.ValidString },
                _mock.Object);

            Assert.IsTrue(newConsole.ToString().Contains(Messages.PerformanceCountersCreated));
            _mock.Verify(m => m.Create(TestHelper.ValidString), Times.Once());
            _mock.Verify(m => m.Validate(It.IsAny<string>()), Times.Never());
        }

        [TestMethod]
        public void CreateWithNotValidString()
        {
            _mock.Setup(m => m.Create(TestHelper.NotValidString)).Throws(new ArgumentException());

            Program.MainWithHelper(new string[] { TestHelper.CreateParam, TestHelper.NotValidString },
                _mock.Object);

            Assert.IsTrue(newConsole.ToString().Contains(Messages.PerformanceCountersNotCreated));
            _mock.Verify(m => m.Create(TestHelper.NotValidString), Times.Once());
            _mock.Verify(m => m.Validate(It.IsAny<string>()), Times.Never());
        }
    }
}